package org.bigjava.buff.goods.service.impl;

import com.github.pagehelper.PageHelper;
import org.bigjava.buff.goods.bean.Box;
import org.bigjava.buff.goods.bean.ExteriorAndPrice;
import org.bigjava.buff.goods.bean.Goods;
import org.bigjava.buff.goods.bean.Type;
import org.bigjava.buff.goods.mapper.GoodsMapper;
import org.bigjava.buff.goods.mapper.TypeMapper;
import org.bigjava.buff.goods.service.GoodsService;
import org.bigjava.buff.user.mapper.DealMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private DealMapper dealMapper;



    //打开箱子 获得箱子中的物品以及磨损值外观价格
    public ExteriorAndPrice openBox(Integer boxId,Integer userId) {

        //通过物品箱id来获得物品
        List<Goods> list = this.goodsMapper.getGoods(boxId);

        Integer index = (int)(Math.random() * 17);
        //获得随机物品
        Goods goods = (Goods)list.get(index);

        //获得物品类型的主键id
        Integer typeId = goods.getGoodsType().getId();

        //通过id获得枪械的名称 以及物品的类型父id
        Type type = typeMapper.getTypeById(typeId);

        //先将物品名称保存到名称属性上 再通过类型父id来获取物品名称
        type.setName(type.getTypeName());

        //获取物品类型并且保存到属性中
        String typeName = typeMapper.getTypeById(type.getFid()).getTypeName();
        type.setTypeName(typeName);

        //将类型保存到物品对象中
        goods.setGoodsType(type);

        //生成18位随机磨损
        String abrasion = "";
        while (abrasion.length() != 18){
            abrasion = String.valueOf(Math.random());
        }

        //磨损值
        Double abValue = Double.valueOf(abrasion);

        //根据磨损来生成不同外观
      /*  薪新出厂：0-0.06
        略有磨损：0.07-0.14  
        久经沙场：0.15-0.37
        跛损不堪：0.38-0.44
        战痕累累：0.45-0.99*/

        ExteriorAndPrice ep = null;
        if (abValue < 0.07D) {
            System.out.println("崭新出厂");
            ep = new ExteriorAndPrice(goods, abrasion, "崭新出厂", null, null);
        } else if (abValue >= 0.07D && abValue < 0.15D) {
            System.out.println("略有磨损");
            ep = new ExteriorAndPrice(goods, abrasion, "略有磨损", null, null);
        } else if (abValue >= 0.15D && abValue < 0.38D) {
            System.out.println("久经沙场");
            ep = new ExteriorAndPrice(goods, abrasion, "久经沙场", null, null);
        } else if (abValue >= 0.38D && abValue < 0.45D) {
            System.out.println("破损不堪");
            ep = new ExteriorAndPrice(goods, abrasion, "破损不堪", null, null);
        } else {
            System.out.println("战痕累累");
            ep = new ExteriorAndPrice(goods, abrasion, "战痕累累", null, null);
        }

        ExteriorAndPrice exp = this.generatedReferencePrice(ep);

        //保存饰品
        goodsMapper.addGoodsExterior(exp);
        //获得刚刚保存物品的id
        Integer exterId = goodsMapper.getLastExterior().getId();

        //将饰品添加到用户库存中
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("exterId", exterId);
        goodsMapper.addInventory(map);

        //返回刚刚抽取到的饰品
        return exp;

    }


    public List<Box> getAllBox() {
        return goodsMapper.getAllBox();
    }

    public List<Goods> showGoodsInBox(Integer id) {

        List<Goods> list =  goodsMapper.getGoods(id);
        for (Goods g : list) {
            Type goodsType = this.typeMapper.getTypeById(g.getGoodsType().getId());
            goodsType.setName(goodsType.getTypeName());
            String typeName = this.typeMapper.getTypeById(goodsType.getFid()).getTypeName();
            goodsType.setTypeName(typeName);
            g.getGoodsType().setTypeName(typeName);
            g.setGoodsType(goodsType);
        }

        return list;
    }

    public int addBox(List<Goods> goodsList, Box box) {

        //创建武器箱
        goodsMapper.createBox(box);

        //获得刚刚创建的武器箱
        Box b = goodsMapper.getBoxByBoxName(box.getBoxName());
        for(Goods goods : goodsList){
            //将武器箱填充到物品里
            goods.setBox(b);
            //循环保存
            goodsMapper.addGoods(goods);
        }

        return b.getId();
    }

    /**
     * 获取物品详细信息
     * @param epId
     * @return  物品详细信息
     */
    public ExteriorAndPrice getGoodsInfo(Integer epId) {
        ExteriorAndPrice ep = goodsMapper.getGoodsInfo(epId);
        Type type = ep.getGoods().getGoodsType();
        type.setName(type.getTypeName());
        type.setTypeName(typeMapper.getTypeById(type.getFid()).getTypeName());
        return ep;
    }

    /**
     * 修改用户库存指向
     * @param uid   新用户的id
     * @param eid   饰品id
     * @return 受到影响行数
     */
    public int updateGoodsUser(Integer uid, Integer eid) {
        return goodsMapper.updateGoodsInventory(uid, eid);
    }


    //生成参考价格
    public ExteriorAndPrice generatedReferencePrice(ExteriorAndPrice ep){



        double priceCh = 0.0;
        double priceUs = 0.0;
        double dollarCurrencyRate = 6.3697;  //美元和人民币的汇率   ￥1 = $6.3697  2022.4.13

        priceCh = Math.random() * 99;

        String str = String.format("%.1f", priceCh);
        priceCh = Double.parseDouble(str);

        ep.setPriceCh(priceCh);
        ep.setPriceUs(priceUs * dollarCurrencyRate);
        return ep;

    }





}
